<?php
/**
  * Musashi - A project manager for music groups and bands
  * Copyright (C) 2010 Daniel Torres
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  */

/**
 * Default view for adding equipment to a song
 *  
 * @package com_musashi
 * @author Daniel Torres
 */
 
// Make sure we have access to this page
defined('_JEXEC') or die ('Restricted access. Sorry');
 
// Add the stylesheet for this page
JHTML::_('stylesheet','modal_styles','administrator/components/com_musashi/views/catalog/tmpl/');

// We'll be needing this model
$model =& $this->getModel();

// Get information about available equipment pieces for this song:
//  select data from equipment pieces not already included in the equipment_song
//  list for this song. Extract the type names.
$songID = JRequest::getVar('idSong',-1);
$sql = "SELECT e.id, e.name, t.name ".
       "FROM #__Musashi_Equipment as e ".
       "INNER JOIN #__Musashi_EquipmentType as t ON e.type = t.id ".
       "WHERE e.id NOT IN ".
        "(".
            "SELECT idEquipment ".
            "FROM #__Musashi_Song_Equipment ".
            "WHERE idSong = $songID".
        ")";
       
$model->_db->setQuery( $sql );
$equipmentList = $model->_db->loadRowList();

if( is_null($equipmentList) || count($equipmentList) == 0 )
{
    ?>
        <div class="modal-header">
            There is no equipment available for this song. This can happen
            if you have added all available equipment, or no equipment pieces
            have been defined yet. 
        </div>
    <?php
}
else
{?>

<script language="javascript" type="text/javascript">
<!--

function validateForm()
{
    form = document.newEquipmentForm;
    if( form.idEquipment.value == "" )
    {
        alert("Please select an equipment piece");
        return false;
    }
    
    if( form.quantity.value == "" )
    {
        alert("Please indicate an amount");
        return false;
    }
    
    addedQuantity = parseInt(form.quantity.value);
    if( addedQuantity == 0)
    {
        alert("Please add one or more elements");
        return false;
    }
    
    // Refresh the parent
    window.top.location.reload(true);
    
    // Close this window
    //window.top.setTimeout('window.parent.document.getElementById(\'sbox-window\').close()', 200);
    return true;
    
}

-->
</script>
<div class="modal-header">Add Equipment</div>
<div class="modal-form">
    <form method="post" action="<?php JRoute::_('index.php'); ?>" name="newEquipmentForm"  onsubmit="return validateForm()" >
        <ol>
            <li>
                <label for="equipment">Equipment piece</label>
                <select name="idEquipment" id="idEquipment" size="7" style="width:25em;">
                    <?php
                        foreach( $equipmentList as $equipmentEntry )
                        {?>
                            <option value="<?php echo $equipmentEntry[0]; ?>"><?php echo $equipmentEntry[2].': '.$equipmentEntry[1]; ?></option>                            
                        <?php
                        }                 
                    ?>                    
                </select>                
            </li>
            <li>
                <label for="quantity">Quantity</label>
                <input type="text" id="quantity" name="quantity" value="1" />
            </li>            
            <li>
                <div class="modal-form-right">
                    <input type="submit" name="submit" value="Add"/>
                </div>
            </li>
        </ol>
    
        <input type="hidden" name="option" value="com_musashi" />
        <input type="hidden" name="task" value="addEquipmentToSong" />
        <input type="hidden" name="controller" value="catalog" />
        <input type="hidden" name="idSong" value="<?php echo $songID; ?>" />
        <input type="hidden"  name="sectn" value="songs" />
        
    </form>
</div>
<?php
}





